/*
 * @Author: liyongxuan
 * @Date: 2018-1-10 10:37:42
 * @Last Modified by: liyongxuan
 * @Last Modified time: 2018-1-10 10:37:42
 * @description项目管理-拟投资-tab组件
 */
<template>
    <div>
        <div class="right__detail">
            <csHeaderEdit
                class="ca-box ca-p-20 ca-m-t-30"
                :itemId="itemId"
                :headerDataEdit="headerData"
                :itemIndustryEntity="itemIndustryEntity"
                :proManage="projectManage"
                :quasiInvestmentFund="quasiInvestmentFund"
                :isAdd="true"
                :showBtn="showBtn"
                :projectTeamUsers="projectTeamUsers"
                @save="save"
                @getMyQuasiInvestmentFund="getMyQuasiInvestmentFund"
                @getMyProManage="getMyProManage"
                @getMyProjectTeamUsers="getMyProjectTeamUsers">
            </csHeaderEdit>
            <csProDetailEdit class="ca-box ca-p-20 ca-m-t-20" :itemId="itemId" :showBtn="showBtn" @region="region" :proDetailEdit="itemQuasiEntity"  :officeAddress="officeAddress" :registeredAddress="registeredAddress"></csProDetailEdit>
        </div>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from './api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    export default {
        data() {
            return {
                itemId: this.$route.params.itemId,
                teamNum: null,//项目组成员
                proMan: null,//项目经理
                projectManage: '',//项目经理
                quasiInvestmentFund: [],//拟投资基金
                projectTeamUsers: [],
                registeredAddress: {
                    id: '',
                    businessId: this.itemId,
                    province: '',
                    city: '',
                    district: '',
                    businessType: 1,
                    registrationDetailed: ''
                },
                officeAddress: {
                    id: '',
                    businessId: this.itemId,
                    province: '',
                    city: '',
                    district: '',
                    businessType: 2,
                    registrationDetailed: ''
                },
                headerData: {
                    'exitEntityList': [], // 拟退出方式
                    'heldStatus': 1,//持有状态：0-潜在投资，1-拟投资，2-持有中，3-减持，4-已退出 ,
                    'id': '',
                    'itemFullname': '',//项目全称
                    'itemImg': '',
                    'itemInfo': '',//项目简介
                    'itemName': '',//项目简称
                    'itemType': 0,
                    'itemId': '',
                    'itemIndustryEntity': [],// 行业
                    'itemRelEntityList': [],// 项目关联（关联持有中项目，拟投资基金等）
                    'quasiUserRelEntityList': [],//关联用户
                },
                itemIndustryEntity: '',//重构行业
                itemQuasiEntity: {
                    'companyNature': '',//企业性质
                    'companyTel': '',//电话
                    'founder': '',//创始人
                    'id': '',
                    'investAmount': '',//拟投资规模
                    'itemId': '',
                    'itemSource': '',//项目来源/挖掘人
                    'officeAddress': '',//办公地址
                    'paidInCapital': '',//实收资本
                    'prostatus': '',//项目轮次
                    'registeredAddress': '',//注册地址
                    'registeredCapital': '',//注册资本
                    'registrationTime': '',//公司成立时间
                    'remark': '',//备注
                    'securityCode': '',//证券代码
                    'shareholderNum': '',//股东人数
                    'lastUpdate': '', // 最近接触时间
                    'firstContact': '',// 首次接触时间
                    'investRole': '',//投资角色
                    'postValuation': '',//投后估值
                    'preValuation': '',//投前估值
                    'subFundManager': '',//投前估值
                    'fundAssigningDirectors': '',//基金外派懂事
                    'fundExternalSupervisor': '',//基金外派监事
                    'investmentWay': '',//投资方式
                    'valuationMethod': '',//估值方式
                    'jointInvestment': '',//联合投资
                    'other': '',//其他
                }, //项目详情信息
                regionData: [],
                showBtn: true,//是否是持有中项目增持
                userEntityList: this.$store.state.user.department.userEntityList,//用户列表
                routerFromName: ''
            }
        },
        beforeRouteEnter(to, from, next) {
          next(vm => {
            vm.routerFromName = from.name;
          });
        },
        props: {},
        mounted() {
          if (this.itemId !== '-1') {
            this.showBtn = false
            this.getItemQuasiEntityTop()
            this.getItemQuasiEntityDetails()
          }
        },
        methods: {
          // 持有中项目增持 获取头部信息
          async getItemQuasiEntityTop() {
              const { resCode,data } = await apiHttp(api.GET_ITEM_QUASI_ENTITY_TOP, {itemId: this.itemId});
              if (resCode === STATUS_HTTP_SUCCESS) {
                  this.headerData = data
                  this.headerData.itemIndustryEntity = this.headerData.itemIndustryEntity[0]
                  this.projectManage = ''
                  this.projectTeamUsers = []
                  // 获取项目经理
                  if (this.headerData.quasiUserRelEntityList.length > 0) {
                      let proTeamUsers = [],flag = true
                      this.headerData.quasiUserRelEntityList.map(item => {
                          if (item.type === 1) {
                              this.projectManage = item.userId
                          } else if (item.type === 2) {
                              this.userEntityList.map(i => {
                                  if (i.id === item.userId) {
                                      item.userName = i.userName
                                  }
                              })
                              proTeamUsers.push(item)
                          }
                      })
                      proTeamUsers.map(u => {
                          if (this.projectTeamUsers.length > 0) {
                              this.projectTeamUsers.map((i,index) => {
                                  if (i.groupDate === u.groupDate) {
                                      this.projectTeamUsers[index].userId.push(u.userId)
                                      this.projectTeamUsers[index].userName.push(u.userName)
                                      flag = false
                                  }
                              })
                              if (flag) {
                                  this.projectTeamUsers.push({groupDate: u.groupDate, userId: [u.userId],userName: [u.userName], type: 2})
                              }
                              flag = true
                          } else {
                              this.projectTeamUsers.push({
                                  groupDate: u.groupDate,
                                  userId: [u.userId],
                                  userName: [u.userName],
                                  type: 2
                              })
                          }
                      })
                  }
                  // 拟投资者基金
                  if (this.headerData.itemRelEntityList.length > 0) {
                      this.quasiInvestmentFund = []
                      this.headerData.itemRelEntityList.map(item => {
                          if (item.type === 3) {
                              this.quasiInvestmentFund.push(item.dataId)
                          }
                      })
                  }
              }
          },
          // 持有中项目增持 获取项目详情
          async getItemQuasiEntityDetails() {
              const {resCode,data} = await apiHttp(api.GET_ITEM_QUASI_ENTITY_DETAILS, {itemId: this.itemId})
              if (resCode === STATUS_HTTP_SUCCESS) {
                let region = data.region
                  this.itemQuasiEntity = data
                  delete this.itemQuasiEntity.additional
                  delete this.itemQuasiEntity.startDate
                  // delete this.itemQuasiEntity.firstContact
                  // delete this.itemQuasiEntity.lastUpdate
                  delete this.itemQuasiEntity.region
                  if (this.itemQuasiEntity.registrationTime !== '' && this.itemQuasiEntity.registrationTime !== null) {
                      this.itemQuasiEntity.registrationTime = this.$moment(this.itemQuasiEntity.registrationTime).format('YYYY-MM-DD')
                  }
                // 处理地区
                // this.registeredAddress = {}
                // this.officeAddress = {}
                if (region && region.length !== 0) {
                    region.map(item => {
                        if (item.businessType === 1) {
                            this.registeredAddress = item
                        } else if (item.businessType === 2) {
                            this.officeAddress = item
                        }
                    })
                }
              }
          },
          //  拟投资基金
          getMyQuasiInvestmentFund(val) {
            this.headerData.itemRelEntityList = []
            val.map(item => {
              this.headerData.itemRelEntityList.push({dataId: item, type: 3})
            })
          },
          // 项目经理
          getMyProManage(val) {
            this.proMan = val
          },
          // 项目组成员
          getMyProjectTeamUsers(val) {
            this.teamNum = val
          },
          // 保存
          async save() {
            this.headerData.quasiUserRelEntityList = [];
            // 重构项目经理
            if (this.proMan !== null) {
                // let index = 0;
                // let flag = true;
                // this.headerData.quasiUserRelEntityList.map((item,i) => {
                //   if (item.type === 1) {
                //     flag = false
                //     index = i
                //   }
                // })
                // if (!flag) {
                //   this.headerData.quasiUserRelEntityList[index].userId = this.proMan
                // } else {
                  this.headerData.quasiUserRelEntityList.push({userId: this.proMan,type: 1,groupDate: ''})
                // }
            }
            // 重构项目组成员
            if (this.teamNum !== null && this.teamNum.length > 0) {
                this.teamNum.map(u => {
                    u.userId.map(i => {
                        this.headerData.quasiUserRelEntityList.push({
                            groupDate: u.groupDate,
                            userId: i,
                            type: 2
                        })
                    })
                })
            }
            // 如果是持有中增持传给后台id
            if (!this.showBtn && this.routerFromName !== 'project-library') {
                let flag = true
                for (let item of this.headerData.itemRelEntityList) {
                    if (item.type === 2) {
                        item.dataId = this.itemId
                        flag = false
                        break
                    }
                }
                if (flag) {
                    this.headerData.itemRelEntityList.push({
                        dataId: this.itemId,
                        type: 2
                    })
                }
            }
            // 保存参数重构
            let query = {
                exitEntityList: this.headerData.exitEntityList,
                itemEntity: {
                    heldStatus: 1,
                    id: this.headerData.id || '',
                    itemFullname: this.headerData.itemFullname,
                    itemImg: this.headerData.itemImg,
                    itemInfo: this.headerData.itemInfo,
                    itemName: this.headerData.itemName,
                    itemType: this.headerData.itemType,
                },
                itemId: '',
                itemIndustryEntity: [this.headerData.itemIndustryEntity],
                itemQuasiEntity: this.itemQuasiEntity,
                itemRelEntityList: this.headerData.itemRelEntityList,
                quasiUserRelEntityList: this.headerData.quasiUserRelEntityList,
                region: [this.registeredAddress]
            }
            query.itemQuasiEntity.itemId = ''
            query.itemQuasiEntity.investAmount = this.headerData.investAmount || '' //拟投资规模
            if (this.routerFromName === 'project-library') {
              query.itemQuasiEntity.id = ''
            }
            const {resCode} = await apiHttp(api.NEW_ITEM_QUASI_ENTITY, query)
            if (resCode === STATUS_HTTP_SUCCESS) {
                this.$message({message: '保存成功', type: 'success',center: true});
                this.$router.push({name: 'pre-investment-project-list'})
            }
          },
          region(val) {
              this.regionData = val
          },
          // 退出
          exit() {
                this.$confirm('是否退出', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning',
                    showClose: false,
                }).then(() => {
                    this.$router.push({name: 'pre-investment-project-list'})
                })
          }
        },
        components: {
          csHeaderEdit: require('./header/edit'),//拟投资项目详情头部 编辑
          csProDetailEdit: require('./tab/proDetail/edit'),// 项目详情 编辑
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.float-right{
  float: right;
}
</style>
